﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MHPL_DoTrangSuc.Data;

namespace MHPL_DoTrangSuc.ManHinh
{
    public partial class frmQuanLyHoaDon : Form
    {
        KetNoi kn = new KetNoi();
        public static string maDonHang;
        public static string mahoadon;
        public int chiso;
        public frmQuanLyHoaDon()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string str;

            ListViewItem item = listViewHD.SelectedItems[0];


            DialogResult result = MessageBox.Show("Bạn có chắc là muốn xóa Hóa Đơn này khỏi danh sách không ?", "Xóa Hóa Đơn", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                str = "DELETE FROM HoaDon WHERE MaHoaDon='" + item.SubItems[0].Text.Trim() + "'";

                kn.Luu(str);
                MessageBox.Show("Hóa Đơn đã được xóa thành công.", "Xóa Hóa Đơn");
                hienthi_lstHoaDon();
                listviewChiTietHoaDon.Items.Clear();
            }
                     
          
        }
     
        public void Load_ListView(ListView lvwName, DataTable tb)
        {
            listViewHD.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                string[] col = new string[tb.Columns.Count];
                for (int j = 0; j < tb.Columns.Count; j++)
                {
                    col[j] = tb.Rows[i][j].ToString();
                }
                ListViewItem lvItem = new ListViewItem(col);
                lvwName.Items.Add(lvItem);
            }
        }

       private void btnThanhToan_Click(object sender, EventArgs e)
        {
            frmThanhToan_HoaDon frm = new frmThanhToan_HoaDon();
            frm.ShowDialog();
           

            //conn.Luu("UPDATE HoaDon SET TinhTrang=N'Đã Thanh Toán' WHERE HoaDon_ID='" + mahoadon.Trim() + "'");
           // Form frm = new frmrptHoaDon();
           // frm.ShowDialog();
           // conn.Luu("DELETE FROM DichVu_PhongThue WHERE ID_Phong='" + maphong.Trim() + "'");
            //conn.Luu("DELETE FROM ChiTiet_DichVuPhong WHERE ID_Phong='" + maphong.Trim() + "'");
            //conn.Luu("DELETE FROM PhongThue WHERE ID_Phong='" + maphong.Trim() + "'");
        }
        
        private void frmQuanLyHoaDon_Load(object sender, EventArgs e)
        {
            kn.ketnoi();
           
            hienthi_lstHoaDon();
            btnXoa.Enabled = false;
            
            btnThanhToan.Enabled = false;
        }

       public void hienthi_lstHoaDon()
        {
            string str = "SELECT MaHoaDon,NgayLapHoaDon,SoTienThanhToan,MaDonHang,TenQT FROM HoaDon hd, QuanTri qt where hd.TenDNQT=qt.TenDNQT ";
            DataTable tb = new DataTable();
            tb.Load(kn.ThucHienReader(str));
            Load_ListView(listViewHD, tb);
        }

       private void listViewHD_SelectedIndexChanged(object sender, EventArgs e)
       {
           if (listViewHD.SelectedItems.Count == 0)
           {
               return;
           }
           else
           {
               btnXoa.Enabled = true;
               
               btnThanhToan.Enabled = true;
               string str;
               ListViewItem item = listViewHD.SelectedItems[0];
               mahoadon = item.SubItems[0].Text.Trim();
               //str = "SELECT MaHoaDon,TenSP,SoLuongMua,GiaBanSP,GiamGia,SoTienThanhToan FROM SanPham sp,ChiTietDonHang ctdh,DonHang dh, HoaDon hd WHERE hd.MaDonHang=dh.MaDonHang and dh.MaDonHang=ctdh.MaDonHang and ctdh.MaSP=sp.MaSP ";
               str = "SELECT MaHoaDon,NgayLapHoaDon,TenSP,SoLuongMua,GiaBanSP,GiamGia,SoTienThanhToan FROM SanPham sp,ChiTietDonHang ctdh,DonHang dh, HoaDon hd WHERE hd.MaDonHang=dh.MaDonHang and dh.MaDonHang=ctdh.MaDonHang and ctdh.MaSP=sp.MaSP ";
               hienthi_lstChiTiet(str);            
               
               
               }

          
       }
       public void hienthi_lstChiTiet(string str)
       {
           DataTable tb = new DataTable();
           tb.Load(kn.ThucHienReader(str));
           Load_ListViewChiTiet(listviewChiTietHoaDon, tb);
       }

       public void Load_ListViewChiTiet(ListView lvwName, DataTable tb)
       {
           listviewChiTietHoaDon.Items.Clear();
           for (int i = 0; i < tb.Rows.Count; i++)
           {
               string[] col = new string[tb.Columns.Count];
               for (int j = 0; j < tb.Columns.Count; j++)
               {
                   col[j] = tb.Rows[i][j].ToString();
               }
               ListViewItem lvItem = new ListViewItem(col);
               lvwName.Items.Add(lvItem);
           }
       }

       private void btnCapNhat_Click(object sender, EventArgs e)
       {
           frmSua_HoaDon obj = new frmSua_HoaDon();
           obj.ShowDialog();
           //obj.Activate();
          // obj.ShowInTaskbar = false;
          // frmQuanLyHoaDon_Load(sender, e);
       }

       private void btnThemMoi_Click(object sender, EventArgs e)
       {
           frmThem_HoaDon obj = new frmThem_HoaDon();
           obj.ShowDialog();
           obj.Activate();
           obj.ShowInTaskbar = false;
           frmQuanLyHoaDon_Load(sender, e);
       }

       

     

       
    }
}
